﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Configuration;

namespace cn.Util
{
    public static class SendEmail
    {

        static string strHost = String.Empty;
        static string strAccount = String.Empty;
        static string strPwd = String.Empty;
        static string strFrom = String.Empty;
        static string strName = String.Empty;

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">接收方邮件地址</param>
        /// <param name="title">邮件标题</param>
        /// <param name="content">邮件正文内容</param>
        public static bool sendmail(string to, string title, string content)
        {
            //strHost = ConfigurationManager.AppSettings["strHost"].ToString();   //STMP服务器地址
            //strAccount = ConfigurationManager.AppSettings["strAccount"].ToString();   //SMTP服务帐号
            //strPwd = ConfigurationManager.AppSettings["strPwd"].ToString();     //SMTP服务密码
            //strFrom = ConfigurationManager.AppSettings["strFrom"].ToString(); //发送方邮件地址
            //strName = ConfigurationManager.AppSettings["strName"].ToString();//发送方邮件地址显示的名称
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            _smtpClient.Host = strHost; ;//指定SMTP服务器
            _smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//用户名和密码
          /// _smtpClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);//STMP服务器端口号

            MailMessage _mailMessage = new MailMessage(strFrom, to);

            _mailMessage.Subject = title;//主题
            _mailMessage.Body = content;//内容
            _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
            _mailMessage.IsBodyHtml = true;//设置为HTML格式
            _mailMessage.Priority = MailPriority.High;//优先级
            _mailMessage.From = new MailAddress(strFrom, strName);
            try
            {
                _smtpClient.Send(_mailMessage);
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
